<?php

namespace IDock\Insurance\ZMBX\Request;

use GuzzleHttp\Psr7\Stream;
use IDock\Http\RequestFormat;
use IDock\IDock\IDockEnums;
use IDock\Insurance\ZMBX\ZMBXEnums;
use IDock\Insurance\ZMBX\Request\AllZMBXRequest;
use IDock\Insurance\ZMBX\Response\UploadFileResponse;
use IDock\Visitor\Premium\PremiumInquiryVisitor;
use IDock\Visitor\UploadFileVisitor;

class UploadFileRequest extends AllZMBXRequest
{
    public $route = 'UploadImage.do';
    public $requestCode = 'UploadImage';

    protected $response = UploadFileResponse::class;

    protected $urlTip = '影像上传';

    protected $inLog = true;

    /**
     * @var  UploadFileVisitor $visitor
     */
    protected $visitor;

    public function generate(): array
    {
        $this->setZMBXToken($this->visitor->argc);
        $file = $this->visitor->fileList;
        $imgList = [];
        foreach ($file as $value){
            $fileType = ZMBXEnums::$uploadFileType[$value['type']] ?? '';
            if(isset($imgList[$fileType])){
                $imgList[$fileType]['imageBeanList'][] = [
                    'fileName' => basename($value['url']),
                    'image' => base64_encode(file_get_contents($value['url'])),
                ];
            }else{
                $imgList[$fileType] = [
                    'id' => $fileType,
                    'imageBeanList' => [
                        [
                            'fileName' => basename($value['url']),
                            'image' => base64_encode(file_get_contents($value['url'])),
                        ]
                    ],
                ];
            }
        }
        $imgList = array_values($imgList);
        $arr = [
            'nodeBeanlist' => $imgList,
            'contractNo' => $this->visitor->inquiryNo
        ];
        return $arr;
    }

}
